//
//  ExampleCalloutView.m
//  UILbrary
//
//  Created by mac on 17/3/9.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ExampleCalloutView.h"
#import <MapKit/MapKit.h>

/// Callout that shows title and subtitle
///
/// This is concrete subclass of `CalloutView` that has two labels. Note, to
/// have the callout resized appropriately, all this class needed to do was
/// update is the constraints between these two labels (which have intrinsic
/// sizes based upon the text contained therein) and the `contentView`.
/// Autolayout takes care of everything else.
///
/// Note, I've added observers for the `title` and `subtitle` properties of
/// the annotation view. Generally you don't need to worry about that, but it
/// can be useful if you're retrieving details about the annotation asynchronously
/// but you want to show the pin while that's happening. You just want to make sure
/// that when the annotation's relevant properties are retrieved, that we update
/// this callout view (if it's being shown at all).

@interface ExampleCalloutView()
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 副标题 */
@property (nonatomic, strong) UILabel *subTitleLabel;
/** 按钮 */
@property (nonatomic, strong) UIButton *detailsButton;

@end

@implementation ExampleCalloutView

#pragma mark - init
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithAnnotation:(MKShape *)annotation{
    self = [super init];

    
    
    [self updateContents:annotation];
    [self config];
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3.0);
    self.subTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.frame.size.height, self.frame.size.height / 3.0);
    self.detailsButton.frame = CGRectMake(0, CGRectGetMaxY(self.subTitleLabel.frame), self.frame.size.width, self.frame.size.height / 3.0);
}
#pragma mark - private methods

- (void)updateContents:(MKShape *)annotation{
    self.titleLabel.text = annotation.title;
    self.titleLabel.text = annotation.subtitle;
}

/// Add constraints for subviews of `contentView`
- (void)config{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subTitleLabel];
    [self.contentView addSubview:self.detailsButton];
    [self.detailsButton addTarget:self action:@selector(didTapDetailsButton) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - event respons

- (void)didTouchUpInCallout{
    NSLog(@"didTouchUpInCallout");
}

- (void)didTapDetailsButton{
    NSLog(@"didTapDetailsButton");
}
#pragma mark - lazy methods
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout];
    }
    return _titleLabel;
}

- (UILabel *)subTitleLabel{
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _subTitleLabel.textColor = [UIColor whiteColor];
        _subTitleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout];
    }
    return _subTitleLabel;
}

- (UIButton *)detailsButton{
    if (!_detailsButton) {
        _detailsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _detailsButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_detailsButton setTitle:@"Details" forState:UIControlStateNormal];
        [_detailsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_detailsButton setBackgroundColor:[UIColor lightGrayColor]];
        _detailsButton.layer.cornerRadius = 3;
    }
    return _detailsButton;
}
@end
